﻿using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform
{
    /// <summary>
    /// 消息发送记录
    /// </summary>
    public class MsgSendRecService : PlatformAppService, IMsgSendRecService
    {
        private readonly IRepository<EventMsgSendRec, Guid> _msgSendRecRepository;
        private readonly MsgSendRecManager _msgSendRecManager;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msgSendRecRepository"></param>
        /// <param name="hubContext"></param>
        public MsgSendRecService(IRepository<EventMsgSendRec, Guid> msgSendRecRepository
            ,MsgSendRecManager msgSendRecManager)
        {
            _msgSendRecRepository = msgSendRecRepository;
            _msgSendRecManager = msgSendRecManager;
        }

        /// <summary>
        /// 获取消息发送记录
        /// </summary>
        /// <returns></returns>
        public async Task<PagedResultDto<MsgSendRecDto>> GetListAsync(MsgSendRecInput input)
        {
            var iQuery = await _msgSendRecRepository.GetQueryableAsync();
            //iQuery = iQuery.WhereIf(!string.IsNullOrEmpty(input.Filter), i => i.OrganName.Contains(input.Filter));

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<EventMsgSendRec>, List<MsgSendRecDto>>(iQuery.ToList());

                return new PagedResultDto<MsgSendRecDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<EventMsgSendRec>, List<MsgSendRecDto>>(items.ToList());

                return new PagedResultDto<MsgSendRecDto>(totalCount, itemsDto);
            }
        }

        ///// <summary>
        ///// 
        ///// </summary>
        ///// <returns></returns>
        //public async Task<MsgSendRecDto> PostAsync(MsgSendRecCreateDto input)
        //{
        //    var msgSendRec = await _msgSendRecManager.CreateMsgSendRec(input.MsgBizTypeCodg, input.MsgName
        //        , input.MsgCont, EnumMsgLv.Information);
        //    msgSendRec = ObjectMapper.Map<MsgSendRecCreateDto, EventMsgSendRec>(input, msgSendRec);
        //    msgSendRec = await _msgSendRecRepository.InsertAsync(msgSendRec);
        //    await _hubContext.Clients.All.SendAsync("ReceiveMessage", msgSendRec.MsgCont);

        //    return ObjectMapper.Map<EventMsgSendRec, MsgSendRecDto>(msgSendRec);
        //}
    }
}
